package io.renren.modules.bs.dao;

import io.renren.modules.bs.entity.BsDepartmentEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import io.renren.modules.bs.vo.BsDepartmentTreeVo;
import io.renren.modules.bs.vo.BsDepartmentVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * 部门表
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2019-08-13 22:35:07
 */
@Mapper
public interface BsDepartmentDao extends BaseMapper<BsDepartmentEntity> {

    @Select("<script>" +
            "SELECT  count(1) FROM bs_department dept\n" +
            "LEFT JOIN bs_hospital hosp on dept.hospital_id = hosp.hospital_id\n" +
            "where 1=1\n" +
            "<if test='hospitalId!=null and hospitalId!=\"\"'>" +
            "and dept.hospital_id = #{hospitalId} \n" +
            "</if>" +
            "</script>")
    Integer getCount(Map<String, Object> params);

    @Select("<script>" +
            "SELECT  dept.*,hosp.hospital_name FROM bs_department dept\n" +
            "LEFT JOIN bs_hospital hosp on dept.hospital_id = hosp.hospital_id\n" +
            "where 1=1\n" +
            "<if test='hospitalId!=null and hospitalId!=\"\"'>" +
            "and dept.hospital_id = #{hospitalId} \n" +
            "</if>" +
            " limit #{start},#{limit}" +
            "</script>")
    List<BsDepartmentVo> list(Map<String, Object> params);

    @Select("SELECT count(1) FROM  bs_department dp \n" +
            "left JOIN bs_department_patient dpa ON dpa.department_id = dp.department_id\n" +
            "WHERE dp.department_id = #{dataValue} AND dpa.leave_hospital_date >= date_format(NOW(),'%Y-%m-%d')")
    Integer getExtractableNumber(@Param("dataValue")Long dataValue);

    @Select("SELECT dep.*,count(dpa.department_patient_id) available_number FROM  bs_department dep \n" +
            "left JOIN bs_department_patient dpa ON dpa.department_id = dep.department_id and dpa.is_leave=0\n" +
            "WHERE dep.hospital_id = #{hospitalId}\n" +
            "GROUP BY dep.department_id")
    List<BsDepartmentTreeVo> getTreeData(@Param("hospitalId") Long hospitalId);
}
